package com.potato.base.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.potato.base.base.BasePresenter;
import com.potato.base.mvp.IView;

import java.util.List;

public abstract class LazyFragment <P extends BasePresenter>extends Fragment  implements IView {

    private boolean isViewCreated;
    private boolean currentVisibleState = false;
    private boolean mIsFirstVisible = true;
    private View rootView;

    private P mPresenter;
    public abstract P loadPresenter();
    public abstract void initData();

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(layoutRes(), container, false);
        initView(rootView);
        initEvent();
        mPresenter=loadPresenter();
        if (mPresenter!=null){
            mPresenter.attachView(this);
        }
        //b表示view已经创建
        isViewCreated = true;
        if (!isHidden() && getUserVisibleHint()) {
            dispatchUserVisibleHint(true);
        }
        return rootView;
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //已经创建
        if (isViewCreated) {
            //分发可执行
            if (!currentVisibleState && isVisibleToUser) {
                dispatchUserVisibleHint(true);
            } else if (currentVisibleState && !isVisibleToUser) {
                dispatchUserVisibleHint(false);
            }
        }
    }

    //分发事件
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void dispatchUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isParentInvisible()) {
            return;
        }
        //状态异常
        if (isVisibleToUser == currentVisibleState)
            return;
        //可见
        if (isVisibleToUser) {
            if (mIsFirstVisible) {
                mIsFirstVisible = false;
                onFragmentFristResume();
            }
            onFragmentResume();
            dispatchChildVisibleState(true);
        } else {//不可见
            onFragmentPause();
            dispatchChildVisibleState(false);
        }
    }

    private boolean isParentInvisible() {
        Fragment pf = getParentFragment();
        if (pf instanceof LazyFragment) {
            LazyFragment lazyFragment = (LazyFragment) pf;
            return !lazyFragment.isSupportVisible();
        }
        return false;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void dispatchChildVisibleState(boolean visible) {
        FragmentManager fragmentManager = getChildFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                if (fragment instanceof LazyFragment &&
                        !fragment.isHidden() &&
                        fragment.getUserVisibleHint()) {
                    ((LazyFragment) fragment).dispatchUserVisibleHint(visible);
                }
            }
        }
    }

    private boolean isSupportVisible() {
        return currentVisibleState;
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onResume() {
        super.onResume();
        if (!mIsFirstVisible) {
            if (getUserVisibleHint() && !currentVisibleState && !isHidden()) {
                dispatchUserVisibleHint(true);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onPause() {
        super.onPause();
        if (getUserVisibleHint() && currentVisibleState) {
            dispatchUserVisibleHint(false);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            dispatchUserVisibleHint(false);
        } else {
            dispatchUserVisibleHint(true);
        }
    }

    public void onFragmentFristResume() {
        initData();
    }

    public void onFragmentPause() {
    }

    public void onFragmentResume() {
    }
    public void initEvent(){

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter!=null)
            mPresenter.detachView();
        isViewCreated = false;
        mIsFirstVisible = false;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
    //加载的view
    public abstract @LayoutRes int layoutRes();

    public abstract void initView(View view);
}
